package com.booking.action;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.Session;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.booking.bean.Food;
import com.booking.bean.Food_PageBean;
import com.booking.bean.Foodorder;
import com.booking.bean.PageBean;
import com.booking.bean.Seller;
import com.booking.bean.Shop;
import com.booking.service.FoodQueryService;
import com.booking.service.OrderService;
import com.booking.service.SellerQueryService;
import com.booking.service.ShopService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import net.sf.json.JSONArray;


@Controller
public class ShopAction extends ActionSupport  implements ModelDriven<Shop>{
	
	//模型驱动使用的对象
	private Shop shop = new Shop();
	@Override
	public Shop getModel() {
		return shop;
	}
	
	@Autowired
	private FoodQueryService foodQueryService;
	@Autowired
	private ShopService shopService;
	@Autowired
	private OrderService orderService;
	

	public String  addFood(){
	
		//1. 根据id获取相应的食品数据  
		Food product = foodQueryService.findById(shop.getFood().getId());
		
		//Session session = ActionContext.getContext().getSession();
		
		//2. 判断当前session是否有购物车，如果没有则创建  
        if(ActionContext.getContext().getSession().get("forder") == null) {  
            //创建新的购物车，存储到session中  
        	ActionContext.getContext().getSession().put("forder", new Foodorder());  
        }   
        
        //3. 把商品信息转化为shop,并且添加到购物车中（判断购物项是否重复）  
        Foodorder forder = (Foodorder) ActionContext.getContext().getSession().get("forder");  
        forder = shopService.addSorder(forder, product);  
          
        //4. 计算购物的总价格  
        forder.setTotalmount(orderService.cluTotal(forder));  
        //5. 把新的购物车存储到session中  
        ActionContext.getContext().getSession().put("forder", forder); 
        int sellerid = product.getSellerid();
        ActionContext.getContext().getSession().put("forder", forder); 
        ActionContext.getContext().getSession().put("sellerid", sellerid); 
        
        return "cart";  
		
	}
	public String updateFood() {
		
		Foodorder forder = (Foodorder) ActionContext.getContext().getSession().get("forder");
		forder = shopService.updateSorder(shop, forder);
		
		BigDecimal  total = orderService.cluTotal(forder);
		forder.setTotalmount(total);
		ActionContext.getContext().getSession().put("forder", forder);
		
		//ByteArrayInputStream  inputStream = new ByteArrayInputStream(forder.getTotalmount().toString().getBytes());
		return "cart";
	}
	public String deleteFood() {
		
		Foodorder forder = (Foodorder) ActionContext.getContext().getSession().get("forder");
		forder = shopService.deleteSorder(shop, forder);
		
		ActionContext.getContext().getSession().put("forder", forder);
		
		//ByteArrayInputStream  inputStream = new ByteArrayInputStream(forder.getTotalmount().toString().getBytes());
		return "cart";
	}
	public String deleteAll() {
/*		BigDecimal total = new BigDecimal(0.00);
		Foodorder forder = (Foodorder) ActionContext.getContext().getSession().get("forder");
		forder = shopService.deleteAll(shop, forder);*/
		ActionContext.getContext().getSession().remove("forder");
		
	/*	forder.setTotalmount(total);
		ActionContext.getContext().getSession().put("forder", forder);*/
		
		//ByteArrayInputStream  inputStream = new ByteArrayInputStream(forder.getTotalmount().toString().getBytes());
		return "cart";
	}
	public String queryShop(){
		List<Shop> result = shopService.queryShop(shop.getOrderid());
		
		ActionContext.getContext().getSession().put("shop",result);

		return "details";
	}
	public String queryOrDetial() throws IOException{
		List<Shop> result = shopService.queryShop(shop.getOrderid());
			
		for(Shop shop :result){
			BigDecimal total = new BigDecimal(0.00);
				total = total.add(shop.getPrice().multiply(new BigDecimal(shop.getNum())));
			shop.setTotalmount(total);
		}
		  JSONArray object = JSONArray.fromObject(result);
          HttpServletResponse response = ServletActionContext.getResponse();
          response.setContentType("html/text;charset=UTF-8");
          response.getWriter().write(object.toString());  
        
          return null;  
	}
}
